import {Injectable} from '@angular/core';
import {NzMessageService, NzNotificationService} from "ng-zorro-antd";
import {AjaxService} from "../../public/service/ajax.service";
import {SettingUrl} from "../../public/setting/setting_url";
import {HttpCodesEnum} from "../../public/setting/enums";

@Injectable({
  providedIn: 'root'
})
export class StarService {

  constructor(private notification: NzNotificationService,
              private ajaxService: AjaxService,
              private message: NzMessageService) {
  }

  /**
   * 查询StarLevel列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getStarLevelList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.starLevel.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 添加StarLevel
   * @param params
   * @returns {Promise<T>}
   */
  addStarLevel(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.post({
        url: SettingUrl.URL.starLevel.add,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改StarLevel
   * @param params
   * @returns {Promise<T>}
   */
  modifyStarLevel(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.starLevel.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改StarLevel状态
   * @param code 供应商编码
   * @param state 供应商状态
   * @returns {Promise<T>}
   */
  modifyStarLevelState(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.starLevel.updateState,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询StarLevel详细信息
   * @param code StarLevel编码
   * @returns {Promise<T>}
   */
  loadStarLevelByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.starLevel.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询StarLevelRule列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getStarLevelRuleList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.starLevelRule.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 添加StarLevelRule
   * @param params
   * @returns {Promise<T>}
   */
  addStarLevelRule(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.post({
        url: SettingUrl.URL.starLevelRule.add,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改StarLevelRule
   * @param params
   * @returns {Promise<T>}
   */
  modifyStarLevelRule(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.starLevelRule.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改StarLevelRule状态
   * @param code 供应商编码
   * @param state 供应商状态
   * @returns {Promise<T>}
   */
  modifyStarLevelRuleState(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.starLevelRule.updateState,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询StarLevelRule详细信息
   * @param code StarLevelRule编码
   * @returns {Promise<T>}
   */
  loadStarLevelRule(id) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.starLevelRule.load + id,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询StarRecord列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getStarRecordList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.starRecord.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询StarRecord详细信息
   * @param code StarRecord编码
   * @returns {Promise<T>}
   */
  loadStarRecordByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.starRecord.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

}
